package cn.pku.ss.fa.ui.component;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Path;
import org.zkoss.zk.ui.Session;
import org.zkoss.zul.Div;
import org.zkoss.zul.Window;

import java.util.LinkedList;
import java.util.List;

import javax.naming.InitialContext;

import cn.pku.ss.fa.bean.FriendLittleBean;
import cn.pku.ss.fa.sessionbean.UserManagerLocal;
import cn.pku.ss.fa.ui.Common;

/**
 * @author SirX
 */
public class FriendsComponent extends Div {
	
	public static void openFriendPage(int friendId) {
        Executions.getCurrent().sendRedirect( "/friend" , "_blank" );
	}
	
	public static int getFriendIdInFriendPage() {
		return (Integer)Path.getComponent("//menuPage/menu").getAttribute("friendId");
	}
	
	public static final int FRIENDS_PER_PAGE = 5;

	public FriendsComponent() {
		Session session = Executions.getCurrent().getDesktop().getSession();
		this.userId = (Integer)session.getAttribute(Common.USER_ID);
		this.initFriendsList();
	}
	
	public List<FriendLittleBean> getFriendsList() {
		return friendsList;
	}
	
	public List<List<FriendLittleBean>> getPagedFriendsList() {
		if( this.pagedFriendsList==null ) {
			this.initPagedFriendsList();
		}
		return this.pagedFriendsList;
	}
	
	public void displayNextColumn() {
		Component currentColumn = this.getCurrentColumn();
		//set next visible:
		if( currentColumn!=null && currentColumn.getNextSibling()!=null ) {
			currentColumn.setVisible(false);
			currentColumn.getNextSibling().setVisible(true);
		}
	}
	
	public void displayPreviousColumn() {
		Component currentColumn = this.getCurrentColumn();
		//set previous visible:
		if( currentColumn!=null && currentColumn.getPreviousSibling()!=null ) {
			currentColumn.setVisible(false);
			currentColumn.getPreviousSibling().setVisible(true);
		}
	}
	
	public void onSearchFriend(String name) {
		Window window = (Window)Executions.createComponents("/main/searchFriendsResult.zul", null, null);
		try {
			window.doModal();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//////////////////////////////////////////////////////////////////////////////
	
	private void initFriendsList() {
		try {
			InitialContext ctx = new InitialContext();
			UserManagerLocal userDao = (UserManagerLocal) ctx
					.lookup("FriendsAnywhere/UserManager/local");
			this.friendsList = userDao.getFriends( this.userId );
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
//		this.friendsList = new LinkedList<FriendLittleBean>();
//		//for test:
//		for(int i=0; i<50; i++) {
//			FriendLittleBean bean = new FriendLittleBean();
//			bean.setId(i);
//			bean.setName("name"+i);
//			bean.setCity("city"+i);
//			bean.setProvince("province"+i);
//			bean.setUniversityName("universityName"+i);
//			bean.setStatus(i+"statusssssssssssssssssssssssssssssssssssssssssssssssssssssssssss");
//			bean.setImageURL("/image/status.png");
//			bean.setPageURL("http://www.g.cn");
//			this.friendsList.add(bean);
//		}
	}
	
	private void initPagedFriendsList() {
		this.pagedFriendsList = new LinkedList<List<FriendLittleBean>>();
		int friendsNumber = this.friendsList.size();
		for(int i=0; i<friendsNumber; i+=FRIENDS_PER_PAGE) {
			int end = i+FRIENDS_PER_PAGE;
			if( end>=friendsNumber ) {
				end = friendsNumber;
			}
			this.pagedFriendsList.add( this.friendsList.subList(i, end) );
		}
	}
	
	@SuppressWarnings("unchecked")
	private Component getCurrentColumn() {
		Component columnLayout = Path.getComponent("//friendsPage/fiendsColumns");
		List<Component> columnList = columnLayout.getChildren();
		Component currentColumn = null;
		for (Component column : columnList) {
			if( column.isVisible() ) {
				currentColumn = column;  //found
				break;
			}
		}
		return currentColumn;
	}
	
	//////////////////////////////////////////////////////////////////////////////

	private int userId;
	private List<FriendLittleBean> friendsList;
	private List<List<FriendLittleBean>> pagedFriendsList;

}
